package com.qf.fronted.controller;

import com.qf.admin.domain.Address;
import com.qf.admin.domain.User;
import com.qf.admin.service.IAddressService;
import com.qf.commons.Data;
import com.qf.fronted.service.AddAddressService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/addressServlet", name = "FrontedAddressServlet")
@CrossOrigin("*")
public class AddressController{
    private AddAddressService addAddressService;
    private IAddressService addressService;

    public AddressController(AddAddressService addAddressService, IAddressService addressServiceImpl) {
        this.addAddressService = addAddressService;
        this.addressService = addressServiceImpl;
    }

    /**
     * 修改用户的收货地址
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    @GetMapping("/changeTakeDeliveryAddress")
    private Data changeTakeDeliveryAddress(Integer addressId,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();

        Data data = null;
        try {
            addressService.changeTakeDeliveryAddress(id, addressId);
            data = new Data(1, "success");
        } catch (Exception ex) {
            ex.printStackTrace();
            data = new Data(-1, "更改收货地址错误");
        }
        return data;
    }

    /**
     * 获取当前用户的收货地址
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping("/getAddressOfUser")
    private List<Address> getAddressOfUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();

        // 获取当前用户的收货地址
        List<Address> addresses = addressService.getAddressByUserId(userId);
        return addresses;
    }

    @GetMapping("/addAddress")
    private void addAddress(String name,String address,String phone,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();  //获取用户id
        // 获取当前用户的收货地址
        addAddressService.addAddress(userId, name, address, phone);

    }

    @GetMapping("/deleteAddress")
    private Data deleteAddress(String addressId,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 需要更改的收货地址
        HttpSession session = req.getSession();

        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();

        Data data = null;
        try {
            addressService.deleteAddress(userId, Integer.parseInt(addressId));
            data = new Data(1, "success");
        } catch (Exception ex) {
            ex.printStackTrace();
            data = new Data(-1, "删除收货地址错误");
        }
        return data;
    }

    @PostMapping("/changeDefalutAddress")
    private Data changeDefalutAddress(String name,String address,String phone,String defaultStatus,String defaultId,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 需要更改的收货地址
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Data data = null;
        try {
            addressService.updateAddress(userId,name,phone,address, Integer.parseInt(defaultStatus), Integer.parseInt(defaultId));
            data = new Data(1, "success");
        } catch (Exception ex) {
            ex.printStackTrace();
            data = new Data(-1, "删除收货地址错误");
        }
       return data;

    }

}
